package com.common.widget.form;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.R;
import com.common.utility.CommonUtility;


public class FormTextView extends RelativeLayout implements View.OnClickListener, View.OnLongClickListener {

    private String mTagString;
    private String mTextString;
    private Boolean mShowDivider;

    private boolean mClickable;//是否能被点击


    private TextView mTagTextView;
    private TextView mTextTextView;
    private View mDividerView;

    private View mContentView;

    private int mActionType;//动作类型
    private ImageView mImageView;//显示图片（电话 邮箱 地图）

    public static final int TYPE_NONE = -1;
    public static final int TYPE_MOBILE = TYPE_NONE + 1;
    public static final int TYPE_MESSAGE = TYPE_MOBILE + 1;
    public static final int TYPE_TEL = TYPE_MESSAGE + 1;
    public static final int TYPE_WEB = TYPE_TEL + 1;
    public static final int TYPE_EMAIL = TYPE_WEB + 1;
    public static final int TYPE_LOCATION = TYPE_EMAIL + 1;
    private boolean mHasArrow;

    public FormTextView(Context context) {
        super(context);
        init(null, 0);
    }

    public FormTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public FormTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.FormView, defStyle, 0);

        mTagString = a.getString(
                R.styleable.FormView_form_tag);
        mTextString = a.getString(
                R.styleable.FormView_form_text);
        mShowDivider = a.getBoolean(
                R.styleable.FormView_form_divider, true);
        mActionType = a.getInt(R.styleable.FormView_form_action_type, TYPE_NONE);

        mClickable = a.getBoolean(R.styleable.FormView_form_clickable, false);

        mHasArrow = a.getBoolean(R.styleable.FormView_form_has_arrow,false);


        a.recycle();

        LayoutInflater.from(getContext()).inflate(R.layout.view_form_text_view, this, true);
        mContentView = findViewById(R.id.form_content);
        mTagTextView = (TextView) findViewById(R.id.form_tag);
        mTextTextView = (TextView) findViewById(R.id.form_text);
        mImageView = (ImageView) findViewById(R.id.form_image_view);
        mDividerView = findViewById(R.id.form_divider);

        if (mTagString != null) {
            mTagTextView.setText(mTagString);
        }

        mTagTextView.setTextColor(getResources().getColor(R.color.form_item_tag_text_color));

        if (mTextString != null) {
            mTextTextView.setText(mTextString);
        }
        mImageView.setVisibility(mHasArrow ? View.VISIBLE : View.GONE);
        if (mClickable) {
            mImageView.setOnClickListener(this);
        }
        mTextTextView.setOnLongClickListener(this);
        mTextTextView.setOnClickListener(this);



        mDividerView.setVisibility(mShowDivider ? View.VISIBLE : View.GONE);
    }

    public void setTag(String tag) {
        mTagTextView.setText(tag);
    }

    public void setText(String text) {
        mTextTextView.setText(text);
    }

    public String getText(){
        return mTextTextView.getText().toString();
    }

    public void onClick(View view) {
        final String text = mTextTextView.getText().toString();
        if (TextUtils.isEmpty(text)) {
            switch (mActionType) {
                case TYPE_MOBILE://mobile
//                   AiKeToast.showToast(getContext(),"手机号为空!");
                    break;
                case TYPE_MESSAGE://message
//                    AiKeToast.showToast(getContext(),"手机号为空!");
                    break;
                case TYPE_TEL://tel
//                    AiKeToast.showToast(getContext(),"电话为空!");
                    break;
                case TYPE_WEB://web
//                    AiKeToast.showToast(getContext(),"网址为空!");
                    break;
                case TYPE_EMAIL://email
//                    AiKeToast.showToast(getContext(),"邮箱为空!");
                    break;
            }
            return;
        }

        if(mActionType == TYPE_NONE){
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        switch (mActionType) {
            case TYPE_MOBILE://mobile
                builder.setTitle("拨打手机?");
                builder.setMessage(text);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setData(Uri.parse("tel://" + text));
                        getContext().startActivity(intent);
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                break;
            case TYPE_MESSAGE:
                builder.setTitle("发送短信?");
                builder.setMessage(text);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Uri uri = Uri.parse("smsto:" + text);
                        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                        intent.putExtra("sms_body", "");
                        getContext().startActivity(intent);
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                break;
            case TYPE_TEL://tel
                builder.setTitle("拨打电话?");
                builder.setMessage(text);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setData(Uri.parse("tel://" + text));
                        getContext().startActivity(intent);
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                break;
            case TYPE_WEB://web
                builder.setTitle("打开网页?");
                builder.setMessage(text);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Uri uri = Uri.parse("http://" + text);
                        //通过Uri获得编辑框里的//地址，加上http://是为了用户输入时可以不要输入
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        //建立Intent对象，传入uri
                        getContext().startActivity(intent);
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                break;
            case TYPE_EMAIL://email
                builder.setTitle("发送邮件?");
                builder.setMessage(text);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent data = new Intent(Intent.ACTION_SENDTO);
                        data.setData(Uri.parse("mailto:" + text));
                        data.putExtra(Intent.EXTRA_SUBJECT, "");
                        data.putExtra(Intent.EXTRA_TEXT, "");
                        getContext().startActivity(data);
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                break;
        }

        builder.show();
    }

    @Override
    public boolean onLongClick(View view) {
        String text="";
        if (mTextTextView != null) {
            text = mTextTextView.getText().toString().trim();
        }
        if (TextUtils.isEmpty(text)) {
            return false;
        }
        ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(text);
        CommonUtility.UIUtility.toast(getContext(), R.string.copy_success);
       return false;
}

}
